﻿Module Bootstrapping
    ''' <summary>
    ''' Process every command line specified at launch time
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ProcessCommandLineParameters()
        If My.Application.CommandLineArgs.Count = 0 Then Exit Sub

        Dim i As Integer
        For i = 0 To My.Application.CommandLineArgs.Count - 1
            Dim parameter As String
            parameter = My.Application.CommandLineArgs(i)
            parameter = Trim(parameter)
            If parameter Like "/*=*" Then
                ' Parameter is a Modifier
                Dim command As String
                Dim value As String
                Dim tmpSplitted() As String
                tmpSplitted = Split(parameter, "=")
                If Not tmpSplitted.Length = 2 Then
                    AddLog(" Invalid parameter <" & parameter & "> specified.")
                    GoTo NextLoop
                End If
                command = tmpSplitted(0)
                value = tmpSplitted(1)
                command = Right(command, command.Length - 1) ' Remove the head '/'
                command = LCase(command)
                command = Trim(command)
                value = Trim(value)
                If command = "" Then
                    AddLog(" Invalid parameter <" & parameter & "> specified.")
                    GoTo NextLoop
                End If
                If value = "" Then
                    AddLog(" Invalid parameter <" & parameter & "> specified.")
                    GoTo NextLoop
                End If
                CheckModifierParameter(command, value)
            ElseIf parameter Like "/*" Then
                ' Parameter is a Switch
                Dim switch As String
                switch = parameter
                switch = Trim(switch)
                switch = Right(switch, switch.Length - 1) ' Remove the head '/'
                CheckSwitchParameter(switch)
            Else
                ' Parameter is a filename
                Dim command As String
                Dim value As String
                If InputFilename = "" Then
                    command = "package"
                Else
                    command = "output"
                End If
                value = parameter
                CheckModifierParameter(command, value)
            End If
NextLoop:
        Next
    End Sub
    ''' <summary>
    ''' Checks and applies a Switch parameter (/something)
    ''' </summary>
    ''' <param name="switch">Switch to be applied</param>
    ''' <remarks></remarks>
    Public Sub CheckSwitchParameter(ByVal switch As String)
        Select Case switch
            Case "verbose", "v"
                Verbose = True
            Case "norecursive", "nr"
                RecursiveQuery = False
            Case "help", "h", "?"
                PrintHelpOnly = True
            Case Else
                AddLog("Unrecognized switch <" & switch & ">")
                Exit Sub
        End Select
    End Sub
    ''' <summary>
    ''' Checks and applies a Modifier parameter (/something=value)
    ''' </summary>
    ''' <param name="command">Command that represents a value that will be changed</param>
    ''' <param name="parameter">The new value of command</param>
    ''' <remarks></remarks>
    Public Sub CheckModifierParameter(ByVal command As String, ByVal parameter As String)
        Select Case command
            Case "package", "source", "p"
                ' Specifies the source package for extraction
                InputFilename = parameter
            Case "output", "out", "o"
                ' Specified the output fpath
                If Not OutputPath = "" Then
                    AddLog("Multiple output paths defined, overriding the previous one...")
                End If
                OutputPath = parameter
            Case "root", "r"
                ' Specifies the working path of the application
                Dim di As New IO.DirectoryInfo(parameter)
                If di.Exists = False Then
                    AddLog("Cant set root directory to <" & parameter & ">.")
                    AddLog("Missing directory.")
                Else
                    IO.Directory.SetCurrentDirectory(di.FullName)
                    AddLog("New root directory successfully set.")
                    AddLog(" :" & di.FullName)
                End If
            Case "files", "f"
                ' Specifies which files to extract from the Arphoon Package
                If Not OutputFilter = "" Then
                    AddLog("Multiple output filters defined, overriding the previous one...")
                End If

                OutputFilter = parameter
            Case Else
                AddLog("Unrecognized modifier <" & command & ">")
                Exit Sub
        End Select
    End Sub

    ''' <summary>
    ''' Prints help about the commands supported by the App
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub PrintHelp()
        AddLog("===========")
        AddLog(" Help Page")
        AddLog("===========")
        AddLog()

        ' Application Main Help
        AddLog("No help available yet!. Hey dev! write some code here!")
    End Sub
End Module
